﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Etest.Models;

namespace Etest.Controllers
{
    [Authorize(Roles = "Professor")]
    public class ControlPanelController : Controller
    {
        etestEntities db = new etestEntities("name=etestEntitiesNadvor");

        //
        // GET: /ControlPanel/

        public ActionResult Index()
        {
            //var users = db.Test.Find(4).Users;
            //ovde gi dava i userite, moze da se vidi so debug so breakpoint kaj View()
           
            return RedirectToAction("Create","ControlPanel");
        }

        public ActionResult Create()
        {
            var professorCourses = db.Course.Where(m=>m.UserOnCourse.Where(s=>s.Users.UserName==User.Identity.Name).Any()).ToList();
            ViewBag.courseId = new SelectList(professorCourses, "id", "name");
            ViewBag.professorId = db.Users.Where(m => m.UserName == User.Identity.Name).First().UserId.ToString();

            return View();
        }

        [HttpPost]
        public ActionResult Create(Test test)
        {
            if (ModelState.IsValid)
            {
                db.Test.Add(test);

                //choose all questions of the course for the test
                
                var testQuestions = db.Question.Where(m=>m.courseId==test.courseId).ToList();
                
                //this is temporary, you can change this, this will go to the other virtual machine
                
                

                foreach (var question in testQuestions)
                {
                    db.TestQuestion.Add(new TestQuestion(test.id, question.id));
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            var professorCourses = db.Course.Where(m => m.UserOnCourse.Where(s => s.Users.UserName == User.Identity.Name).Any()).ToList();
            ViewBag.courseId = new SelectList(professorCourses, "id", "name");
            ViewBag.professorId = db.Users.Where(m => m.UserName == User.Identity.Name).First().UserId.ToString();
            return View(test);
        }

        //nova metoda za isprakjanje na novite podatoci nazad na server

    }
}
